/**
 * Auth
 */

import setting from '../setting'
import * as storage from './storage'
import router from '../router'
import store from '../store'

function getRememberMe() {
  return JSON.parse(storage.get(true, 'remember_me'))
}

function setToken(token) {
  storage.set(getRememberMe(), setting.app_token_key, token)
}

function getToken() {
  return storage.get(getRememberMe(), setting.app_token_key)
}

function removeToken() {
  storage.remove(getRememberMe(), setting.app_token_key)
}

function setUserId(id) {
  storage.set(getRememberMe(), 'user_id', id)
}

function getUserId() {
  return storage.get(getRememberMe(), 'user_id')
}

function removeUserId() {
  storage.remove(getRememberMe(), 'user_id')
}

function setUserName(name) {
  storage.set(getRememberMe(), 'user_name', name)
}

function getUserName() {
  return storage.get(getRememberMe(), 'user_name')
}

function removeUserName() {
  storage.remove(getRememberMe(), 'user_name')
}
function removeProjectId() {
  storage.remove(getRememberMe(), 'project_id')
  store.commit("clearAllData")
}
function removeSelectTunnel() {
  window.sessionStorage.removeItem('defaultTunnel')
}

function logOut() {
  removeToken()
  removeUserId()
  removeUserName()
  removeProjectId()
  removeSelectTunnel()
  store.commit('clearAllData', null)
  storage.set(true, 'remember_me', false)
  router.push({ name: setting.app_index_page })
}

export { setToken, getToken, setUserId, getUserId, setUserName, getUserName, logOut }
